//
//  StrangerdataCell.m
//  CheekiOS
//
//  Created by mac on 2017/5/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEditInfodataCell.h"
#import "FWBLayout.h"

@interface UserEditInfodataCell ()
@property (nonatomic, strong) UIImageView *HeadImage;//头像
@property (nonatomic, strong) UIButton *ageBtn;//年龄
@property (nonatomic, strong) UILabel *constellationLab;//星座
@property (nonatomic, strong) UIImageView *coordinatesImg;//位置图片
@property (nonatomic, strong) UILabel *dealiteLab;//位置信息
@property (nonatomic, strong) UIView *bgView;//背景


@property (nonatomic, strong) UIView *userView;//个人View

@property (nonatomic, strong) UILabel *userLab;//标签
@property (nonatomic, strong) UIImageView *userImageColor;//标签色;
@property (nonatomic, strong) UILabel *professionalLab;//职业//目前状态//身高//体重

@property (nonatomic, strong) NSArray *arr;

@property (nonatomic, strong) UIImageView *Editimage;


@end

@implementation UserEditInfodataCell
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bgView];
        
        
        
        self.userView = [[UIView alloc] init];
        self.userView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.userView];
        
        
        
        self.HeadImage = [[UIImageView alloc]init];
        self.HeadImage.contentMode = UIViewContentModeScaleAspectFill;
        [self.HeadImage.layer setMasksToBounds:YES];
        CALayer * layer = [self.HeadImage layer];
        layer.borderColor = [[UIColor whiteColor] CGColor];
        layer.borderWidth = 2.0f;
        [self.bgView addSubview:self.HeadImage];
        
        self.ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bgView addSubview:self.ageBtn];
        self.ageBtn.enabled = NO;
        self.ageBtn.layer.cornerRadius = 4;
        self.ageBtn.layer.masksToBounds = YES;
        [self.ageBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
        self.ageBtn .imageEdgeInsets = UIEdgeInsetsMake(0,-5,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
        self.ageBtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
        
        
        
        self.constellationLab = [[UILabel alloc] init];
        [self.bgView addSubview:self.constellationLab];
        _constellationLab.textColor = Theme_Color_White;
        _constellationLab.layer.cornerRadius = 4;
        _constellationLab.layer.masksToBounds = YES;
        _constellationLab.backgroundColor = RandomColor;
        
        if (iPhone5) {
            self.constellationLab.font = FontP(12);
            self.ageBtn.titleLabel.font = FontBold(12);
            
        }else{
            self.constellationLab.font = FontP(14);
            self.ageBtn.titleLabel.font = FontBold(14);
        }
        _constellationLab.textAlignment = NSTextAlignmentCenter;
        
        
        self.coordinatesImg = [[UIImageView alloc] init];
        
        [self.bgView addSubview:self.coordinatesImg];
        
        self.dealiteLab = [[UILabel alloc] init];
        self.dealiteLab.font = FontP(12);
        self.dealiteLab.textAlignment = NSTextAlignmentLeft;
        self.dealiteLab.textColor = ColorI(0x992e353f);
        [self.dealiteLab sizeToFit];
        
        [self.bgView addSubview:self.dealiteLab];
        
        _Editimage = [[UIImageView alloc]init];
        
        self.Editimage.image = [UIImage imageNamed:@"ziliao"];
        [self.contentView addSubview:_Editimage];
        
        [self masLayout];
    }
    
    
    return self;
}

- (void)masLayout{
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.right.equalTo(self);
        make.height.mas_equalTo(firstSectionCellH);
    }];
    
    CGFloat headImgH = 65*SCREEN_WIDTH_RATIO47;
    [_HeadImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(15*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(self.bgView).offset(-20*SCREEN_WIDTH_RATIO47);
        make.height.width.mas_equalTo(headImgH);
    }];
    self.HeadImage.layer.cornerRadius = headImgH/2;
    
    
    
    [_ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_HeadImage.mas_right).offset(10*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(self.bgView).offset(15*SCREEN_WIDTH_RATIO47);
    }];
    
    [_constellationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_ageBtn.mas_right).offset(10*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(self.bgView).offset(15*SCREEN_WIDTH_RATIO47);
    }];
    
    [_coordinatesImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_HeadImage.mas_right).offset(10*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_ageBtn.mas_bottom).offset(10*SCREEN_WIDTH_RATIO47);
    }];
    
    [_dealiteLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coordinatesImg.mas_right).offset(10*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(25*SCREEN_WIDTH_RATIO47);
        make.right.mas_equalTo(KScreenWidth*0.6*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_ageBtn.mas_bottom).offset(12*SCREEN_WIDTH_RATIO47);
    }];
    
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self);
        make.left.equalTo(self);
        make.top.equalTo(self.bgView.mas_bottom);
        
    }];
    
    
    [self.Editimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.top.equalTo(self.mas_centerY).offset(-10);
        make.height.width.mas_equalTo(20);
        
    }];
    
}

-(void)setInfoModel:(UserpersionlInfoModel *)infoModel{
    //头像
    _infoModel = infoModel;
 
  

    
    if (_infoModel.skill.length) {
        _arr = [[NSArray alloc] init];
        
        _arr = [_infoModel.skill componentsSeparatedByString:@","];
    }
    
    self.coordinatesImg.image = [UIImage imageNamed:@"placeholder_date"];
    self.dealiteLab.text = [NSString stringWithFormat:@"%@",_infoModel.location];
    [_HeadImage sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(_infoModel.photo)]];
    if (_infoModel.sex == 0) {
        [self.ageBtn setBackgroundColor:ColorI(0xf28484)];
        
        [self.ageBtn setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }else{
        [self.ageBtn setBackgroundColor:ColorI(0x84b9f2)];
        
        [self.ageBtn setImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
    }
    //年龄
    [self.ageBtn setTitle:_infoModel.age  forState:UIControlStateNormal];
    //星座
    
    self.constellationLab.backgroundColor = [self getColorWithStr:_infoModel.star];
    
    self.constellationLab.text = _infoModel.star;
    
    
    for (UIView * subView in self.userView.subviews){
        if ([subView isKindOfClass:[UILabel class]]) {
            [subView removeFromSuperview];
        }
    }
    
    for (UIView *subViewImg in self.userView.subviews) {
        if ([subViewImg isKindOfClass:[UIImageView class]]) {
            [subViewImg removeFromSuperview];
        }
    }
 
    //间距
    CGFloat IUW = 15;
    //当前最大的x
    CGFloat maxX = 0;
    NSArray * colorImgArr = @[@"boy_blue",@"green",@"orange",@"purple"];
    for (int i = 0; i < _arr.count ; i++) {
        _userImageColor = [[UIImageView alloc] init];
        _userLab = [[UILabel alloc] init];
        _userLab.textColor = ColorI(0x992e353f);
        _userLab.font = FontP(12);
        _userLab.text = _arr[i];
        _userLab.tag = i+1000;
        
        CGSize userLabSize = textSizeFont(_userLab.text,_userLab.font, KScreenWidth, oneLineHeight);
        _userImageColor.frame = CGRectMake(maxX+IUW, 15, 10, 10);
        _userLab.frame = CGRectMake(_userImageColor.right+5,  0, userLabSize.width, 40);
        _userImageColor.image = [UIImage imageNamed:colorImgArr[i%colorImgArr.count]];
        
        [self.userView addSubview:_userImageColor];
        [self.userView addSubview:_userLab];
        maxX = CGRectGetMaxX(_userLab.frame) ;
    }
    
    
    NSMutableArray *infomodearr = [NSMutableArray array];
    if (_infoModel.job != nil) {
        [infomodearr addObject:_infoModel.job];
    }
    if (_infoModel.affection != nil) {
        [infomodearr addObject:_infoModel.affection];
    }
    if (_infoModel.height != nil) {
        [infomodearr addObject:_infoModel.height];
    }
    if (_infoModel.weight != nil) {
        [infomodearr addObject:_infoModel.weight];
    }
    
    
    //当前最大的x
    CGFloat maxXa = self.left;
    CGFloat fessionLblY = (_userLab) ? _userLab.bottom : 3;
    for (int i = 0; i <infomodearr.count; i++) {
        _professionalLab = [[UILabel alloc] init];
        _professionalLab.textColor =ColorI(0x992e353f);
        _professionalLab.font = FontP(14);
        _professionalLab.text = infomodearr[i];
        
        CGSize profeesionSize = textSizeFont(_professionalLab.text, _professionalLab.font, KScreenWidth, 40);
        _professionalLab.frame = CGRectMake(IUW+maxXa, fessionLblY,profeesionSize.width , 40);
        
        
        [self.userView addSubview:_professionalLab];
        maxXa = CGRectGetMaxX(_professionalLab.frame);
        
    }
    
}
- (UIColor *)getColorWithStr:(NSString *)starStr{
    
    NSArray * starArr = @[@"白羊座",@"天秤座",@"金牛座",@"天蝎座",@"双子座",@"射手座",@"巨蟹座",@"摩羯座",@"狮子座",@"处女座",@"双鱼座"];
    NSArray * colorArr = @[ColorI(0xd80808),ColorI(0xec3db0),ColorI(0x2898df),ColorI(0xa651c9),ColorI(0xff6839),ColorI(0x77ca69),ColorI(0xd16739),ColorI(0x30d3d5),ColorI(0x19d7ad),ColorI(0xecac3d),ColorI(0x32b576)];
    
    
    for (NSString * star in starArr) {
        if ([star isEqualToString:starStr]) {
            NSInteger index = [starArr indexOfObject:star];
            if (colorArr[index]) {
                return colorArr[index];
            }
        }
    }
    return ColorI(0xd80808);
}




@end
